package top.zhangjianyong.accesslog;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import top.zhangjianyong.accesslog.entity.Log;
import top.zhangjianyong.accesslog.utils.IdWorker;
import top.zhangjianyong.accesslog.utils.JDBCUtil;
import top.zhangjianyong.accesslog.utils.RegexUtil;


import java.io.*;
import java.sql.SQLException;

/**
 * 分析nginx日志
 *
 * @author zhangjianyong
 * @since 2024/7/22 上午11:40
 */
@Slf4j
public class AnalyzeLog {

    private final static String regex = "^(\\S*) - (\\S*) \\[([\\S\\s]*)] \"([^\"]*)\" ([0-9]*) ([0-9]*) \"([^\"]*)\" \"([^\"]*)\"";

    private final static IdWorker idWorker = new IdWorker(1, 1, 1);

    static {
        JDBCUtil.init();
    }

    private final static String insSql = "INSERT INTO log(id,remote_addr,remote_user,time_local,request,`status`,body_bytes_sent,http_referer,http_user_agent,md5)\n" + " VALUES(?,?,?,?,?,?,?,?,?,?)";
    private final static String countSql = "SELECT COUNT(1) count FROM `log` WHERE `md5` = ?";

    public static void main(String[] args) {

        String logPath = "/Users/zhangjianyong/Downloads/access_proxy.log";
        InputStream ins = null;
        BufferedReader br = null;
        try {
            ins = new FileInputStream(logPath);
            br = new BufferedReader(new InputStreamReader(ins));

            br.lines()
                    .filter(StrUtil::isNotBlank)
                    .map(line -> RegexUtil.matchesAll(line, regex))
                    .limit(1).map(list -> new Log(list, idWorker.nextId()))
                    .filter(l -> {
                        try {
                            return (long) JDBCUtil.qryObj(countSql, l.getMd5()).get("count") == 0;
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }).forEach(l -> {
                        //插入数据
                        try {
                            /*(id,remote_addr,remote_user,time_local,request,`status`,body_bytes_sent,http_referer,http_user_agent,md5)*/
                            JDBCUtil.insert(insSql, l.getId(),
                                    l.getRemoteAddr(), l.getRemoteUser(),
                                    l.getTimeLocal(), l.getRequest(), l.getStatus(),
                                    l.getBodyBytesSent(), l.getHttpReferer(),
                                    l.getHttpUserAgent(), l.getMd5());
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    });

        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
        } finally {
            IoUtil.close(ins);
            IoUtil.close(br);
        }
    }
}
